<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="模型名称">
        <el-input v-model="queryParams.name" size="small" style="width: 240px" placeholder="请输入模型名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="标识">
        <el-input v-model="queryParams.key" size="small" style="width: 240px" placeholder="请输入标识" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['activiti:process:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="模型名称" align="center" prop="name" show-overflow-tooltip />
      <el-table-column label="标识" align="center" prop="key" show-overflow-tooltip />
      <el-table-column label="版本" align="center" prop="version" show-overflow-tooltip />
      <el-table-column label="描述" align="center" prop="description" show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['activiti:process:update']" size="mini" type="text" icon="el-icon-edit" @click="handleDesign(scope.row.id)">在线设计</el-button>
          <el-button v-hasPermission="['activiti:process:update']" size="mini" type="text" icon="el-icon-edit" @click="handleDeploy(scope.row.id)">部署</el-button>
          <el-button v-hasPermission="['activiti:process:update']" size="mini" type="text" icon="el-icon-edit" @click="handleExport(scope.row.id)">导出</el-button>
          <el-button v-hasPermission="['activiti:process:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="modelFormDialog.title" :visible.sync="modelFormDialog.visible" :close-on-click-modal="false" width="500px" append-to-body>
      <el-form ref="modelForm" :model="modelFormData" :rules="modelFormRules" label-width="80px">
        <el-form-item label="模型名称" prop="name">
          <el-input v-model="modelFormData.name" placeholder="请输入标识" />
        </el-form-item>
        <el-form-item label="标识" prop="key">
          <el-input v-model="modelFormData.key" placeholder="请输入标识" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="modelFormData.description" type="textarea" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmModelFormDialog">确 定</el-button>
        <el-button @click="resetModelFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ModelApi from '@/api/activiti/ModelApi'
import AuthUtils from '@/utils/AuthUtils'
import qs from 'qs'

const defaultModelFormData = {
  id: null,
  name: null,
  key: null,
  description: null
}
export default {
  data() {
    return {
      options: {
      },
      queryParams: {
        name: null,
        key: null
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      modelFormDialog: {
        title: '',
        visible: false
      },
      modelFormData: Object.assign({}, defaultModelFormData),
      modelFormRules: {
        name: [{ required: true, message: '模型名称不能为空', trigger: 'blur' }],
        key: [{ required: true, message: '标识不能为空', trigger: 'blur' }]
      },
      dateRange: [],
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
  },
  methods: {
    handleAdd() {
      this.openModelFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return ModelApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleQuery() {
      this.loading = true
      ModelApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport(id) {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return ModelApi.export(id)
      }).then(response => {
        this.$common.download(response.data)
      })
    },
    // 在线设计
    handleDesign(id) {
      const params = qs.stringify({
        'token': AuthUtils.getToken(),
        'modelId': id
      })
      window.open(process.env.VUE_APP_BASE_API + `/modeler.html?${params}`)
    },
    // 部署
    handleDeploy(id) {
      this.$confirm('确定进行部署操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        ModelApi.deploy(id).then(() => {
          this.handleQuery()
          this.$message.success('操作成功')
        })
      }).catch(() => {})
    },
    openModelFormDialog(id) {
      this.resetModelFormDialog()
      this.modelFormDialog.visible = true
      this.modelFormDialog.title = '模型-添加'
    },
    confirmModelFormDialog() {
      this.$refs['modelForm'].validate(valid => {
        if (valid) {
          if (this.modelFormData.id) {
            ModelApi.edit(this.modelFormData).then(response => {
              this.$message.success('修改成功')
              this.modelFormDialog.visible = false
              this.handleQuery()
            })
          } else {
            ModelApi.add(this.modelFormData).then(response => {
              this.$message.success('添加成功')
              this.modelFormDialog.visible = false
              this.handleQuery()
            })
          }
        }
      })
    },
    resetModelFormDialog() {
      this.modelFormDialog = {
        title: '',
        visible: false
      }
      this.modelFormData = Object.assign({}, defaultModelFormData)
      this.resetForm('modelForm')
    },
    resetQuery() {
      this.page.current = 1
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    }
  }
}
</script>
